<?php


namespace app\controller\common;

use app\common\Util;
use App\model\AppModel;
use app\service\Route;
use app\common\Verify;
use think\facade\Cache;

class Common
{
    /**
     * @Route(reqUrl="/common.verify")
     */
    public function verify(){
        $token = input("token");
        $code = new Verify(array("seKey"=>$token,"imageH"=>"32","imageW"=>"100",'useCurve'=> false,'useNoise'=>false,'length'=>4,'fontSize'=>14,'bg'=>array(213,224,233)));
        $img = $code->entry();
        ob_start();
        imagepng($img);
        $fileContent = ob_get_contents();
        ob_end_clean();
        return response("data:image/png;base64," . base64_encode($fileContent), 200, ['Content-Type' => 'image/jpeg']);
    }

    /**
     * @Route(reqUrl="/common.captcha.send")
     */
    public function sendCaptcha(){
        $mobile=input('mobile');
        $appid=input('appid');
        $app=AppModel::where([['app_id','=',$appid]])->find();
        $code=Util::randString(4);
        Cache::set($mobile.'_captcha',$code,300);
        $res = Sms::sendCaptcha($mobile,$code,$app['sms_sign_name'],$app['sms_template_code'],$app['sms_config']);
        return $res['Message']=='OK' ? success() : error(1,"发送失败");
    }

}